<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input v-model="form.account" style="width: 370px;" />
      </el-form-item>
      <el-form-item label="密码" prop="password" >
        <el-input type="password"
            placeholder="请输入新密码" 
            maxlength="18"  
　　        :show-password="true" 
            v-model="form.password" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="手机号码" prop="cellphone">
        <el-input v-model="form.cellphone" style="width: 370px;"/>
      </el-form-item>
     <el-form-item label="所属客户" prop="customerId">
       <el-select v-model="form.customerId" placeholder="所属客户" v-on:change="indexSelect($event)">
           <el-option v-for="(item,index) in customerItem" :key="index" :label="item.customerName" :value="item.id">
           </el-option>
      </el-select>
      </el-form-item>
      <!-- <el-form-item label="所属客户id" >
        <el-input v-model="form.customerId" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="所属客户" >
        <el-input v-model="form.customerName" style="width: 370px;"/>
      </el-form-item> -->
      <!-- <el-form-item label="创建时间" >
        <el-input v-model="form.creatTime" style="width: 370px;"/>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" v-show="isView===true" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'
import { add, edit } from '@/api/tCustomerUser'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    isView: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false, dialog: false, 
      form: {
        id: '',
        username: '',
        account:'',
        password: '',
        cellphone: '',
        customerId: '',
        customerName: '',
        creatTime: ''
      },
      rules: {
        username: [
          { required: true, message: "请输入姓名", trigger: "blur" }
        ],
        account: [
          { required: true, message: "请输入账号", trigger: "blur" }
        ],
        cellphone: [
          { required: true, message: "请输入电话号码", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" }
        ],
        customerId: [
          { required: true, message: "请选择所属客户", trigger: "blur" }
        ]
      }
    }
  },
  mounted:function(){
     this.tcustmerInfoAll();
  },
  methods: {
      tcustmerInfoAll(){
        this.getAllcustmer().then(res => {
          this.customerItem=res;
        //根据id再进行全部查询
      })
    },
    getAllcustmer() {
      return request({
        url: 'api/tCustomerInfoAll',
        method: 'get'
      })
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
     this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.isAdd) {
            this.doAdd();
          } else this.doEdit();
        } else {
          return false;
        }
      });
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        username: '',
        account:'',
        password: '',
        cellphone: '',
        customerId: '',
        customerName: '',
        creatTime: ''
      }
    }
  }
}

</script>

<style scoped>

</style>
